<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
</head>

<body>
    <!-- 
        请补全JavaScript代码，根据预设代码中的数组，实现以下功能：
        1. 列表只展示数组中的name属性
        2. 实现点击"销量升序"按钮，列表内容按照销量升序重新渲染
        3. 实现点击"销量降序"按钮，列表内容按照销量降序重新渲染
        注意：
        1. 必须使用DOM0级标准事件（onclick）
     -->
    <button class='up'>销量升序</button>
    <button class='down'>销量降序</button>
    <ul></ul>

    <script>
        var cups = [
            { type: 1, price: 100, color: 'black', sales: 3000, name: '牛客logo马克杯' },
            { type: 2, price: 40, color: 'blue', sales: 1000, name: '无盖星空杯' },
            { type: 4, price: 60, color: 'green', sales: 200, name: '老式茶杯' },
            { type: 3, price: 50, color: 'green', sales: 600, name: '欧式印花杯' }
        ]
        var ul = document.querySelector('ul');
        var upbtn = document.querySelector('.up');
        var downbtn = document.querySelector('.down');
        // 补全代码
        function show() {
            while (ul.firstChild) {
                ul.removeChild(ul.firstChild);
            }
            for (let i = 0; i < cups.length; i++) {
                let newLi = document.createElement("li");
                newLi.textContent = cups[i].name;
                ul.appendChild(newLi)
            }
        }
        //show();
        upbtn.onclick = () => {
            //升序
            for (let j = 0; j < cups.length; j++) {
                let min = cups[j].price;
                let minDom = cups[j];
                for (let i = j + 1; i < cups.length; i++) {
                    if (cups[i].price < cups[j].price) {
                        min = cups[i].price;
                        minDom = cups[i];
                        cups[i] = cups[j];
                        cups[j] = minDom;
                    }
                }
            }
            show();
        }

        downbtn.onclick = () => {
            // 降序
            for (let j = 0; j < cups.length; j++) {
                let max = cups[j].price;
                let maxDom = cups[j];
                for (let i = j + 1; i < cups.length; i++) {
                    if (cups[i].price > cups[j].price) {
                        max = cups[i].price;
                        maxDom = cups[i];
                        cups[i] = cups[j];
                        cups[j] = maxDom;
                    }
                }
            }
            show();
        }
        // const _listRendering = arr => {
        //     var str = ''
        //     arr.forEach(element => {
        //         str += `<li>${element.name}</li>`
        //     })
        //     ul.innerHTML = str
        // }
        // upbtn.onclick = function () {
        //     var upArr = cups.sort(function (a, b) {
        //         return a.sales - b.sales
        //     })
        //     _listRendering(upArr)
        // }
        // downbtn.onclick = function () {
        //     var downArr = cups.sort(function (a, b) {
        //         return b.sales - a.sales
        //     })
        //     _listRendering(downArr)
        // }


    </script>
</body>

</html>